// pages/goods/goods-add.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addInfo: true,
    alterInfo: false,

    phoneVal: '',
    nameVal: '',
    address: '',

    addBox: true,

    buyAdd: 0,

  },

  bindDelete(e) {
    const that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var list = that.data.list;
    wx.showModal({
      title: '确定删除该地址吗?',
      content: '删除后无法还原',
      showCancel: true,
      cancelText: '想一想',
      cancelColor: '',
      confirmText: '确定',
      confirmColor: '',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.url + 'DelAddress',
            method: 'POST',
            data: {
              ad_id: id,
            },
            success: function (res) {
              if (res.data.info) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'none',
                  duration: 1000,
                  mask: true,
                })
                that.getAddressPage()
              }
            },
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '删除失败',
          icon: 'none',
          duration: 1000,
          mask: true,
        })
      },
      complete: function (res) {},
    })
  },

  bindmodify(e) {
    const that = this;
    var item = e.currentTarget.dataset.item;
    console.log(item)
    that.setData({
      ad_id: item.id,
      nameVal: item.name,
      phoneVal: item.phone,
      address: item.address,
      addInfo: false,
      alterInfo: true,
      addBox: false,
    })
  },

  bindAdd(e) {
    const that = this;
    that.setData({
      addInfo: false,
      alterInfo: true,
      phoneVal: '',
      nameVal: '',
      address: '',
    })
  },

  bindName(e) {
    this.setData({
      nameVal: e.detail.value.trim()
    })
  },

  bindPhone(e) {
    this.setData({
      phoneVal: e.detail.value.trim()
    })
  },

  bindAddress(e) {
    this.setData({
      address: e.detail.value.trim()
    })
  },

  bindOpenadd(e) {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res)
        that.setData({
          lat: res.latitude,
          lng: res.longitude,
          address: res.address,
        })
      }
    })
  },

  getLocations(e) {
    const that = this;
    wx.getSetting({
      success: (res) => {
        console.log(res)
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) { //非初始化进入该页面,且未授权
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权，否则无法获取您所需数据',
            success: function (res) {
              if (res.cancel) {
                that.setData({
                  isshowCIty: false
                })
                wx.showToast({
                  title: '授权失败',
                  icon: "loading",
                  duration: 1200
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1200
                      })
                      //再次授权，调用getLocationt的API
                      that.bindOpenadd();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'success',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) { //初始化进入
          that.bindOpenadd();
        } else { //授权后默认加载
          that.bindOpenadd();
        }
      }
    })
  },

  bindUsetInfo(e) {
    var that = this;
    if (e.detail.userInfo) {
      that.setData({
        userInfo: e.detail.userInfo
      })
      app.globalData.userInfo = e.detail.userInfo;
      wx.request({
        url: app.globalData.url + 'Obtain',
        data: {
          openid: app.globalData.openid,
          wx_name: app.globalData.userInfo.nickName,
          wx_img: app.globalData.userInfo.avatarUrl,
        },
        method: 'POST',
        success: function (res) {
          that.bindSubmit()
        },
      })
    }
  },

  bindServe(e) {
    const that = this;
    const phone_reg = /^1(3|4|5|6|7|8|9)\d{9}$/;
    if (that.data.nameVal == '') {
      wx.showToast({
        title: '请输入正确的姓名',
        icon: 'none',
        duration: 1000,
        mask: true,
      })
      return
    } else if (that.data.phoneVal == '' || !phone_reg.test(that.data.phoneVal)) {
      wx.showToast({
        title: '请输入正确的电话',
        icon: 'none',
        duration: 1000,
        mask: true,
      })
      return
    } else if (that.data.address == '' || !phone_reg.test(that.data.phoneVal)) {
      wx.showToast({
        title: '请输入正确的地址',
        icon: 'none',
        duration: 1000,
        mask: true,
      })
      return
    } else {
      wx.showLoading({
        title: '正在保存中',
        mask: true,
      })
      wx.request({
        url: app.globalData.url + 'Addressadd',
        method: 'POST',
        data: {
          uid: app.globalData.uid,
          name: that.data.nameVal,
          phone: that.data.phoneVal,
          address: that.data.address,
          lat: that.data.lat,
          lng: that.data.lng,
          ad_id: that.data.ad_id,
        },
        success: function (res) {
          if (res.data.info) {
            that.setData({
              alterInfo: false,
              addInfo: true,
              addBox: true,
            })
            that.getAddressPage()
          }else{
            that.setData({
              alterInfo: false,
              addInfo: true,
              addBox: true,
            })
          }
          wx.hideLoading()
        },
      })
    }
  },

  getAddressPage(e) {
    const that = this;
    wx.showLoading({
      title: '玩命加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.url + 'AddressPage',
      method: 'POST',
      data: {
        uid: app.globalData.uid,
      },
      success: function (res) {
        if (res.data.info) {
          that.setData({
            addInfo: res.data.info.address,
          })
        }
        wx.hideLoading()
      },
    })
  },

  bindBuyAdd(e) {
    var ad_id = e.currentTarget.dataset.id;
    app.globalData.ad_id = ad_id;
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    if (options.buyAdd) {
      that.setData({
        buyAdd: options.buyAdd
      })
    }
    that.getAddressPage();
    wx.stopPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getAddressPage();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})